<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template v-slot:count_info="{row}">
      <ul>
        <li v-for="v in row.value">{{v.title}}：{{v.num}}</li>
      </ul>
    </template>
  </forms>
</template>

<script>
import s94Mjs from "s94-js";
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import {decode_config} from "@/js/s94_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let forms_config = {
      title: {label:'标题',name:'title',type:'text_one',tips:''},
      agent_category_ids: {label:'代理类目',name:'agent_category_ids',type:'list_ids',config:'category/index',tips:'', value:''},
      head_img: {label:'头图',name:'head_img',type:'file_img',tips:'作者介绍-头图'},
      keyword: {label:'关键词',name:'keyword',type:'text_one',tips:'多个逗号分隔'},
      info: {label:'简介',name:'info',type:'text_more',tips:''},

      area: {label:'所在地区',name:'area',type:'text_one',tips:'格式：省份 市 区县。例如：重庆市 重庆市 江北区'},
      address: {label:'详细地址',name:'address',type:'text_one',tips:''},
      position: {label:'具体位置',name:'position',type:'position',tips:'', onchange: this.get_address},
      link_name: {label:'联系人姓名',name:'link_name',type:'text_one',tips:''},
      link_phone: {label:'联系人电话',name:'link_phone',type:'text_one',tips:''},
      must_file: {label:'身份证明',name:'must_file',type:'file_imgs',tips:'身份证明，个人用户一般为身份证，商户为营业执照'},
      file: {label:'附件',name:'file',type:'file_imgs',tips:'其他附件图片'},
      sort: {label:'排序',name:'sort',type:'text_one',config:'number',tips:'排序索引，大>小', value:'0'},
    }
    if (id){
      forms_config = Object.assign({
        phone: {label:'用户电话',name:'phone',tips:''},
        nickname: {label:'用户昵称',name:'nickname',tips:''},
      }, forms_config);
      Object.assign(forms_config, {
        extend_data: {label:'其他参数',name:'extend_data',type:'json_param', is_info:true, value:''},
        count: {label:'作品统计',name:'count',type:'count_info',value:[]},
        last_ip: {label:'最后登录IP',name:'last_ip'},
        last_time: {label:'最后登录时间',name:'last_time'},
        create_time: {label:'入驻时间',name:'create_time',tips:''},
      })
    }else {
      forms_config = Object.assign({
        user_id: {label:'选择用户',name:'user_id',type:'list_id',config:'user/index',tips:''},
        admin_role_ids: {label:'角色',name:'admin_role_ids',type:'select_more',config:[],tips:'', value:''},
      }, forms_config);
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/agent/edit?id=`+this.id : '/admin/agent/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('agent/index', '$refs.list_url.list');
          iframe.hide('agent/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
        if (!this.id){
          this.$alert(res.message.replace(/\n/g,'<br>'), {dangerouslyUseHTMLString:true});
          return false;
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
    get_address(row,v){
      let app = this;
      let arr = v.split(',');
      var geocoder = new TMap.service.Geocoder();
      geocoder.getAddress({ location: new TMap.LatLng(arr[1],arr[0]) }).then(function (res){
        let area = res.result.address_component;
        app.config.area.value = area.province+' '+area.city+' '+area.district;
        // app.config.address.value = res.result.formatted_addresses.recommend
      });
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/agent/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('agent/edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
    if (!this.id){
      ajax({url:'/admin/admin_role/list?order[id]=asc',cache:true},function(res){
        app.config.admin_role_ids.config = decode_config(res.data.list, {content_key:'name', value_key:'id'});
        return false
      })
    }
  }
}
</script>
